#include"p2p.h"
int sock_cmp_addr(const struct hostlist *hostl, const struct sockaddr_in *sa)
{
	int i;
	i = memcmp(&(hostl->host), &(sa->sin_addr.s_addr), sizeof(struct in_addr));
	return (i);
}

static char *sock_ntophost(const struct sockaddr_in *sa, int len)
{
	static char str[128];
	if(sa->sin_family == AF_INET)
	{
		if(inet_ntop(AF_INET, &sa->sin_addr, str, sizeof(str)) == NULL)
		{
			return(NULL);
			perror("inet_ntop");
			exit(1);
		}
		return(str);
	}
	exit(1);

}

	
//int main(int arg, char **argv)
void *updataHostlist(void *arg)
{
	int sockfd;
	int i, j;
	char buff[1024];
	struct sockaddr_in localaddr, cliaddr;
	int fromlen = 64;
	int on = 1;
	char message[] = "yong";
	int len;
	extern struct hostlist *hostl[20];

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sockfd < 0)
	{
		perror("socket error");
		exit(1);
	}
	bzero(&localaddr, sizeof(localaddr));
	localaddr.sin_family = AF_INET;
	localaddr.sin_port = htons(8080);
	localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*	i = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
	if (i < 0)
	{
		perror("setsockopt error");
		exit(1);
	}
*/
	i = bind(sockfd, (struct sockaddr *)&localaddr, sizeof(localaddr));
	if(i < 0)
	{
		perror("bind error");
		exit(1);
	}

	for(; ;)   
	{
	i = recvfrom(sockfd, buff, 1023, 0, (struct sockaddr *)&cliaddr, &fromlen);
	if (i < 0)
	{
		perror("recvfrom error");
		exit(1);

	}
	i = strlen(buff);
	buff[i] = '\0';
	printf("%s\n", buff);

	if(strcmp(buff, "qlizhi") == 0)
	{
		len = sizeof(message);
	//	for(; ;)
	//	{
		
		printf("from %s :%s\n", sock_ntophost(&cliaddr,sizeof(cliaddr)), buff);
		for(i = 0; i < 20; i++)
		{
/*			if(flag_first == 0)
				{
					if(hostl[i]->on == 1);
					sendto(sockfd, message, len, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
					flag_first == 1;
				}
	 		else{ */
					if(hostl[i]->on == 0)
					{
					memcpy(&(hostl[i]->host), &(cliaddr.sin_addr.s_addr), sizeof(struct in_addr));
					hostl[i]->on = 1;
					hostl[i]->counts++;
					sendto(sockfd, message, len, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
					printf("add  to hostl[%d]   :\n", i);
					break;
					//  hostl[i]-> size = xxx;	
					}else
					{

			
					j = sock_cmp_addr(hostl[i], &cliaddr);//  0 : same, 1: not same
					printf("the cmp_addr result is %d\n", j);
					if (j == 0)
					{
						hostl[i]->counts++;
						printf("the same ip\n");
						break;
					}
					else if(j != 0)
					{
					
						continue;
					
					}
//				}
			}
		}
	//	sleep(2);
	//	}
	}
	}  //for
	return(NULL);
}




void continueDown(char *urlBuff, int length)
{
extern struct tasklist *taskl[TASK];
int i, j;
int len;
char fseek[64];
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
for(i =0; i < TASK; i++)
{
	if(taskl[i]->flag == 1)
	{
		char message[] = "yong";
	
		len = sizeof(message);
		struct sockaddr_in hostaddr;
			hostaddr.sin_family = AF_INET;
			hostaddr.sin_port = htons(8081);
			hostaddr.sin_addr.s_addr = taskl[i]->host;
		sendto(sockfd, message, len, 0, (struct sockaddr *)&hostaddr, sizeof(struct sockaddr_in));

		j = sendto(sockfd, urlBuff, length, 0, (struct sockaddr *)&hostaddr, sizeof(hostaddr));
		if(j < 0)
		{
			perror("sendto error");
			exit(1);
		}

		bzero(fseek, 64);

		sprintf(fseek, "%d", taskl[i]->flag);
		i = strlen(fseek);
		sprintf(fseek + i, "%d", taskl[i]->task);
		i = strlen(fseek);
		sprintf(fseek + i, "%d", taskl[i]->pfseek);
	
		i = sendto(sockfd, fseek, strlen(fseek), 0, (struct sockaddr *)&hostaddr, sizeof(hostaddr));



	}
}
return;
}
























